//
//  CustomAlertView.swift
//  AleartView
//
//  Created by 郑东喜 on 2016/12/2.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

class CustomAlertView: UIView {
    
    static let shared = CustomAlertView()
    
    var alertView = UIAlertController()
    
    var test:(_ : ()->Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension CustomAlertView {
    

    //显示标题，信息
    func alertWithTitle(strTitle : String) -> Void {

        alertView = UIAlertController.init(title: strTitle, message: nil, preferredStyle: .alert)

        //显示提示框
        UIApplication.shared.keyWindow?.rootViewController?.present(alertView, animated: true, completion: nil)
        
        self.dissmiss()

    }
    
    func closeWithAlert(strTitle : String,test:(_ : ()->Void)?)-> Void {
        
        self.test = test
        alertView = UIAlertController.init(title: strTitle, message: nil, preferredStyle: .alert)
        
        alertView.addAction(UIAlertAction.init(title: "知道了", style: .default, handler: { (nil) in
            test!()
        }))
        
        //显示提示框
        UIApplication.shared.keyWindow?.rootViewController?.present(alertView, animated: true, completion: nil)

    }
    
    func alertAfterDown(strTitle : String) -> Void {
        alertView = UIAlertController.init(title: strTitle, message: nil, preferredStyle: .alert)
        
        //显示提示框
        UIApplication.shared.keyWindow?.rootViewController?.present(alertView, animated: true, completion: nil)
    }
    
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        dissmiss()
    }
    
    // MARK:- 消失
    func dissmiss() -> Void {
        //两秒钟后自动消失
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5) {
            UIApplication.shared.keyWindow?.rootViewController?.dismiss(animated: false, completion: nil)
        }

    
    }

    
}
